<?php
namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller{
    public function index(){
        $this -> display();
    }

    //生成验证码的方法
    public function createCode(){

        $Verify = new \Think\Verify();
        //调整验证码的格式
        $Verify->codeSet = '0123456789';
        $Verify->imageW = 100;
        $Verify->imageH = 30;
        $Verify->fontSize = 14;
        $Verify->length = 4;
        $Verify->useNoise = false;
        $Verify->entry();

    }

    //检测登录的方法
    public function checkLogin(){
        $code = I('post.code');
        //验证码的验证
        $verify = new \Think\Verify();
        $result = $verify->check($code);
        if(!$result){
//            $this->error('验证码错误',U('Login/index'));
            exit($this->ajaxReturn($JSONObject['code'] =3));
        }
        //接受用户提交的信息进行验证
        $user = M('User');
        //数据数组
        $arr['username'] = I('post.username');
        $arr['password'] = md5(I('post.password'));
        $arr['_logic'] = 'AND';
        $arr['type'] = 1;
        $loginUser = $user->where($arr)->find();
        if($loginUser){
            //将用户登录信息写入SESSION
            $_SESSION['isLogin'] = true;//登录标志
            $_SESSION['id'] = $loginUser['id'];//登录id
            $_SESSION['username'] = $loginUser['username'];;//登录名字
            $_SESSION['email'] = $loginUser['email'];;//登录邮箱
//            $this->success('欢迎光临1',U('Index/index'));
            $this->ajaxReturn($JSONObject['code'] =1);
        }else{
            //验证没通过
            //$this->error('用户名密码错误，请重新登录',U('Login/index'));
            exit($this->ajaxReturn($JSONObject['code'] =2));
        }

    }

    //退出方法
    public function logout(){
        //销毁session
        $_SESSION = array();
        setCookie('isLogin',null,time(),'/');
        setCookie('id',null,time(),'/');
        setCookie('username',null,time(),'/');
        setCookie('email',null,time(),'/');
        session_destroy();
        //跳转会登录页面
        redirect("index");

    }

}